package com.hqyj.ctxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.ctxp.mapper.OrderItemMapper;
import com.hqyj.ctxp.mapper.OrdersMapper;
import com.hqyj.ctxp.mapper.ProductMapper;
import com.hqyj.ctxp.mapper.ShopCartMapper;
import com.hqyj.ctxp.pojo.OrderItem;
import com.hqyj.ctxp.pojo.Orders;
import com.hqyj.ctxp.pojo.Product;
import com.hqyj.ctxp.pojo.ShopCart;
import com.hqyj.ctxp.service.OrdersService;
import com.hqyj.ctxp.service.ShopCartService;
import com.hqyj.ctxp.utils.PageBean;
import com.hqyj.ctxp.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private ShopCartMapper ShopCartMapper;

    @Autowired
    private ProductMapper productMapper;

    // 结算 增加订单
    @Override
    @Transactional
    public ResultInfo addOrder(Integer userNo, Double totalPrice, List<OrderItem> orderItems) {

        // 处理订单 orders
       Orders orders = new Orders();
       Long orderNo = System.currentTimeMillis();
       orders.setOrderNo(orderNo);
       orders.setUserNo(userNo);
       orders.setTotalPrice(totalPrice);
       int i = ordersMapper.insert(orders);

       // 处理订单项
        int j = 0;
        for (OrderItem orderItem : orderItems) {
            orderItem.setOrderNo(orderNo);
            int temp = orderItemMapper.insert(orderItem);
            if (temp > 0) {
                j++;
                // 每个订单项目影响 对应商品库
                Product product = new Product();
//                product.setProductId(orderItem.getProductId());
//                product.setNumber(orderItem.getNumber());
                int m = productMapper.update(product  ,new UpdateWrapper<Product>().eq("product_id", orderItem.getProductId())
                        .setSql("number =" + "number" + "-" + orderItem.getNumber()));
                if (m <= 0) {
                    break;
                }
            }
        }

        // 处理购物车 清空
        int n = ShopCartMapper.delete(new QueryWrapper<ShopCart>());
        if (i > 0 && j == orderItems.size() && n >0) {
            return new ResultInfo(200, "add success", j);
        }
        return new ResultInfo(500, "add fail", 0);
    }

    // 根据订单编号删除订单
    @Override
    public ResultInfo deleteOrderById(Integer orderNo) {
        int i = ordersMapper.delete(new QueryWrapper<Orders>().eq("order_no", orderNo));
        if (i != 0) {
            new ResultInfo(200, "delete success", i);
        }
        return  new ResultInfo(500, "delete fail", 0);
    }

    // 管理员根据订单编号删除订单
    @Override
    public ResultInfo deleteOrderByIdAdmin(Integer orderNo) {

        int i = ordersMapper.delete(new QueryWrapper<Orders>().eq("order_no", orderNo));
        int j = orderItemMapper.delete(new QueryWrapper<OrderItem>().eq("order_no", orderNo));
        if (i > 0 && j> 0) {
            return new ResultInfo(200, "delete success", i);
        }
        return  new ResultInfo(500, "delete fail", 0);
    }

    // 查询 当前用户订单
    @Override
    public ResultInfo selectOrders(Integer userNo) {
        List<Orders> orders = ordersMapper.selectOrders(userNo);
        if (orders != null) {
            return new ResultInfo(200, "select success", orders);
        }
        return new ResultInfo(500, "select fail", null);
    }

    // 查询 所有用户订单
    @Override
    public ResultInfo selectOrderPage(PageBean pageBean) {
        Page<Orders> page = new Page<>(pageBean.getCurrentPage(), pageBean.getPageSize());
        Page<Orders> ordersPage = ordersMapper.selectOrderPage(page, pageBean.getKeyword(), pageBean.getSort(), pageBean.getDirection());
        if (ordersPage != null) {
            return new ResultInfo(200, "select success", ordersPage);
        }
        return new ResultInfo(500, "select fail", null);
    }

    // 提交订单 修改订单状态
    @Override
    public ResultInfo submitOrder(Long orderNo, Integer userNo) {
        int i = ordersMapper.update(null, new UpdateWrapper<Orders>()
                .eq("order_no", orderNo)
                .eq("user_no", userNo)
                .setSql("pay_status = 1, order_status = 0"));
        if (i > 0) {
            return new ResultInfo(200, "update success", i);
        }
        return new ResultInfo(500, "update fail", 0);
    }

    // 管理员发货 修改订单状态
    @Override
    public ResultInfo submitOrderAdmin(Long orderNo, Integer userNo) {
        int i = ordersMapper.update(null, new UpdateWrapper<Orders>()
                .eq("order_no", orderNo)
                .eq("user_no", userNo)
                .set("update_time",new Date())
                .setSql("order_status = 1"));
        if (i > 0) {
            return new ResultInfo(200, "update success", i);
        }
        return new ResultInfo(500, "update fail", 0);
    }
}
